home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / PartMaker 4.4 / PartMaker Documents / Script Runner• / Script Runner•.rsrc / dFRK_5046 < prev    next >
Encoding:
Text File  |  1995-12-12  |  12.8 KB  |  499 lines

  1. /*------------------------------------------------------------------------------
  2.     File:        CScripter.cpp
  3.  
  4.     Contains:    The CScripter class implementation.
  5.  
  6.     Written by:    Sue Dumont, Andrey Dolgachev
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. ------------------------------------------------------------------------------*/
  10.  
  11. // -- Compiler/Preprocessor Notification --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- OpenDoc Utilities --
  18.  
  19. #ifndef _EXCEPT_
  20. #include <Except.h>
  21. #endif
  22.  
  23. // -- CScripter Includes --
  24.  
  25. #ifndef _CSCRIPTER_
  26. #include "CScripter.h"
  27. #endif
  28.  
  29. #ifndef SOM_SampleCode_DataTransferExt_xh
  30. #include <DataTransferExt.xh>
  31. #endif
  32.  
  33. // -- OpenDoc Utilities --
  34.  
  35. #ifndef _BARRAY_
  36. #include <BArray.h>
  37. #endif
  38.  
  39. #ifndef _ODMEMORY_
  40. #include <ODMemory.h>
  41. #endif
  42.  
  43. #ifndef _ODUTILS_
  44. #include <ODUtils.h>
  45. #endif
  46.  
  47. #ifndef _ODDESUTL_
  48. #include <ODDesUtl.h>
  49. #endif
  50.  
  51. #ifndef _ISOSTR_
  52. #include <ISOStr.h>
  53. #endif
  54.  
  55. #ifndef _TEMPOBJ_
  56. #include <TempObj.h>
  57. #endif
  58.  
  59.  
  60. // -- Mac Includes --
  61.  
  62. #ifndef __APPLESCRIPT__
  63. #include <AppleScript.h>
  64. #endif
  65.  
  66. #ifndef __COMPONENTS__
  67. #include <Components.h>
  68. #endif
  69.  
  70. #ifndef __OSA__
  71. #include <OSA.h>
  72. #endif
  73.  
  74. #ifndef __ERRORS__
  75. #include <Errors.h>
  76. #endif
  77.  
  78. #pragma segment CScripter
  79.  
  80. //==============================================================================
  81. // CScripter
  82. //==============================================================================
  83.  
  84. //------------------------------------------------------------------------------
  85. // Method:        CScripter
  86. //------------------------------------------------------------------------------
  87. CScripter::CScripter()
  88. {
  89.     fComponent = kODNULL;
  90.     fSource = kODNULL;
  91.     fScriptID = kOSANullScript;
  92.     fResultValue = kOSANullScript;
  93.     fScriptDirty = kODFalse;
  94. }
  95.  
  96. //------------------------------------------------------------------------------
  97. // Method:        ~CScripter
  98. //------------------------------------------------------------------------------
  99. CScripter::~CScripter()
  100. {
  101.     // Dispose of the source data, script data, and result value.
  102.     if ( fSource )
  103.         AEDisposeDesc(fSource);
  104.     if ( fComponent )
  105.     {
  106.         OSADispose((ComponentInstance)fComponent, fScriptID);
  107.         OSADispose((ComponentInstance)fComponent, fResultValue);
  108.  
  109.         // Terminate the connection to the scripting component.
  110.         CloseComponent((ComponentInstance)fComponent);
  111.     }
  112. }
  113.  
  114. //------------------------------------------------------------------------------
  115. // Method:        OpenScriptingComponent
  116. //
  117. // Description:    Open a generic scripting component.
  118. //------------------------------------------------------------------------------
  119. ODScriptingConnection CScripter::OpenScriptingComponent()
  120. {
  121.     TRY
  122.         // We need to open only one connection, so first check to make sure
  123.         // we haven't already done so.
  124.         if ( fComponent == kODNULL )
  125.             fComponent = (ODScriptingConnection) OpenDefaultComponent(kOSAComponentType, 
  126.                                             kOSAGenericScriptingComponentSubtype);
  127.     CATCH_ALL
  128.         RERAISE;
  129.     ENDTRY
  130.     
  131.     return fComponent;
  132. }
  133.  
  134. //------------------------------------------------------------------------------
  135. // Method:        ReleaseScriptData
  136. //------------------------------------------------------------------------------
  137. void CScripter::ReleaseScriptData()
  138. {
  139.     if ( fScriptID != kOSANullScript )   
  140.     {
  141.         OSADispose((ComponentInstance) fComponent, fScriptID);
  142.         fScriptID = kOSANullScript;
  143.     }
  144.  
  145.     fScriptDirty = kODTrue;
  146. }
  147.  
  148. //------------------------------------------------------------------------------
  149. // Method:        GetScriptSourceDesc
  150. //
  151. // Description:    Return an OD descriptor with a reference to the current script source.
  152. //------------------------------------------------------------------------------
  153. ODDesc* CScripter::GetScriptSourceDesc()
  154. {
  155.     ODDesc*    result = kODNULL;
  156.     
  157.     ODVolatile(result);
  158.     
  159.     TRY
  160.         result = new ODDesc;
  161.         AEDescToODDesc(fSource, result);
  162.     CATCH_ALL
  163.         // Dispose of the descriptor record and set the pointer to null.
  164.         ODDeleteObject(result);
  165.         RERAISE;
  166.     ENDTRY
  167.     
  168.     return result;
  169. }
  170.  
  171. //------------------------------------------------------------------------------
  172. // Method:        SetScriptSourceDesc
  173. //
  174. // Description:    Set the current script source with the given descriptor.
  175. //------------------------------------------------------------------------------
  176. void CScripter::SetScriptSourceDesc( ODDesc* source )
  177. {
  178.     TRY
  179.         if ( fSource == kODNULL )
  180.             fSource = new AEDesc;        // Initialize fSource.
  181.         else
  182.             // Deallocate fSource and set it to a null descriptor record.
  183.             AEDisposeDesc(fSource);
  184.             
  185.         ODDescToAEDesc(source, fSource);
  186.     
  187.         // Set the dirty flag to update the script object.
  188.         fScriptDirty = kODTrue;
  189.     CATCH_ALL
  190.         RERAISE;
  191.     ENDTRY        
  192. }
  193.  
  194. //------------------------------------------------------------------------------
  195. // Method:        GetScriptSourceBA
  196. //
  197. // Description:    Attempts to return data in the type requested. Styled text
  198. //                and plain text are supported.
  199. //------------------------------------------------------------------------------
  200. ODBoolean CScripter::GetScriptSourceBA( ODPlatformType wantType, ODByteArray* data )
  201. {
  202.     DescType            descType;
  203.     ODSLong                dataSize, styleSize;
  204.     ODPtr                dataPtr;
  205.     ODBoolean            result         = kODFalse;
  206.     
  207.     TRY
  208.         descType = fSource->descriptorType;
  209.         
  210.         if ( descType == typeStyledText || descType == typeAEText )
  211.         {
  212.             AERecord    record;
  213.         
  214.             TempAEDesc textDesc =  new AEDesc;
  215.             TempAEDesc styleDesc = new AEDesc;
  216.             
  217.             // Coerce to record and extract text and style fields:
  218.             if ( !AECoerceDesc(fSource, typeAERecord, &record) )
  219.             {
  220.                 AEGetKeyDesc(&record, keyAEStyles, typeScrapStyles, styleDesc);
  221.                 AEGetKeyDesc(&record, keyAEText, typeChar, textDesc);
  222.             }
  223.         
  224.             AEDisposeDesc(&record);
  225.  
  226.             if ( wantType == 'stxt' )
  227.             {
  228.                 styleSize = GetHandleSize(styleDesc->dataHandle);
  229.                 dataSize = GetHandleSize(textDesc->dataHandle) + styleSize;
  230.                 
  231.                 // Copy the data into the ODByteArray
  232.                 data->_buffer = (octet*) ODNewPtrClear(dataSize);
  233.         
  234.                 dataPtr = ODLockHandle(styleDesc->dataHandle);
  235.                 ODBlockMove(dataPtr, data->_buffer, styleSize);
  236.                 ODUnlockHandle(styleDesc->dataHandle);
  237.                 dataPtr = ODLockHandle(textDesc->dataHandle);
  238.                 ODBlockMove(dataPtr, data->_buffer+styleSize, dataSize - styleSize);
  239.                 ODUnlockHandle(textDesc->dataHandle);
  240.             
  241.                 data->_length = dataSize;
  242.                 data->_maximum = dataSize;
  243.             
  244.                 result = kODTrue;
  245.             }
  246.             else // return plain text
  247.             {
  248.                 dataSize = GetHandleSize(textDesc->dataHandle);
  249.                 
  250.                 // Copy the data into the ODByteArray
  251.                 data->_buffer = (octet*) ODNewPtrClear(dataSize);
  252.                 dataPtr = ODLockHandle(fSource->dataHandle);
  253.                 ODBlockMove(dataPtr, data->_buffer, dataSize);
  254.                 ODUnlockHandle(fSource->dataHandle);
  255.             
  256.                 data->_length = dataSize;
  257.                 data->_maximum = dataSize;
  258.  
  259.                 result = kODTrue;
  260.             }
  261.     }
  262.     CATCH_ALL
  263.         // Clean up allocated memory.
  264.         if ( data )
  265.             DisposeByteArray(data);
  266.         result = kODFalse;
  267.         RERAISE;
  268.     ENDTRY
  269.  
  270.     return result;
  271. }
  272.  
  273. //------------------------------------------------------------------------------
  274. // Method:        SetScriptSourceBA
  275. //
  276. // Description:    Set the current script source from the specified ODByteArray.
  277. //                The ODByteArray contains the data, data type and size..
  278. //------------------------------------------------------------------------------
  279. void CScripter::SetScriptSourceBA( ODPlatformType type, ODByteArray* source)
  280. {
  281.     ODHandle        data;
  282.     
  283.     TRY
  284.         data = ODNewHandle(source->_length);    
  285.         ODPtr dataPtr = ODLockHandle(data);
  286.         ODBlockMove(source->_buffer, dataPtr,  source->_length);
  287.         ODUnlockHandle(data);
  288.         
  289.         if ( fSource == kODNULL )
  290.             fSource = new AEDesc;        // Initialize fSource.
  291.         else
  292.             // Deallocate fSource and set it to a null descriptor record.
  293.             AEDisposeDesc(fSource);
  294.  
  295.         // Set fSource to the current script data.
  296.         fSource->descriptorType = type;
  297.         fSource->dataHandle = data;
  298.         
  299.         // Set dirty flag to update the script object.
  300.         fScriptDirty = kODTrue;
  301.     CATCH_ALL
  302.         RERAISE;
  303.     ENDTRY
  304. }
  305.  
  306. //------------------------------------------------------------------------------
  307. // Method:        SetOSAScriptID
  308. //------------------------------------------------------------------------------
  309. void CScripter::SetOSAScriptID( ODOSAID scriptID )
  310. {
  311.     // First clean up, if necessary.
  312.     if ( fScriptID != kOSANullScript )   
  313.         OSADispose((ComponentInstance)fComponent, fScriptID);
  314.  
  315.     fScriptID = scriptID;
  316.     fScriptDirty = kODFalse; 
  317. }
  318.  
  319. //------------------------------------------------------------------------------
  320. // Method:        DoCompile
  321. //
  322. // Description:    Compile the script using the current script source
  323. //                and current script object id.
  324. //------------------------------------------------------------------------------
  325. void CScripter::DoCompile()
  326. {
  327.     THROW_IF_ERROR( OSACompile((ComponentInstance)fComponent, fSource,
  328.                              kOSAModeCompileIntoContext, &fScriptID) );
  329.     fScriptDirty = kODFalse;    
  330. }
  331.  
  332. //------------------------------------------------------------------------------
  333. // Method:        DoDecompile
  334. //
  335. // Description:    Decompile the script data into source data.
  336. //------------------------------------------------------------------------------
  337. void CScripter::DoDecompile()
  338. {
  339.     THROW_IF_ERROR( OSAGetSource( (ComponentInstance)fComponent, fScriptID,
  340.                             typeStyledText, fSource) );
  341. }
  342.  
  343. //------------------------------------------------------------------------------
  344. // Method:        DoRun
  345. //------------------------------------------------------------------------------
  346. void CScripter::DoRun()
  347. {
  348.     TRY
  349.         // if the script object is not current, compile it.
  350.         if ( fScriptDirty )    
  351.             this->DoCompile();
  352.             
  353.         // If compilation was successful, then execute the script
  354.         THROW_IF_ERROR( OSAExecute((ComponentInstance)fComponent,
  355.                                      fScriptID,
  356.                                      kOSANullScript,
  357.                                      kOSAModeNull,
  358.                                     &fResultValue) );
  359.     CATCH_ALL
  360.         RERAISE;
  361.     ENDTRY
  362. }
  363.  
  364. //------------------------------------------------------------------------------
  365. // Method:        GetResultAsODDesc
  366. //------------------------------------------------------------------------------
  367. void CScripter::GetResultAsODDesc( ODDescType type, ODDesc* result )
  368. {    
  369.     TRY
  370.         TempAEDesc newDesc = new AEDesc;
  371.     
  372.         // Convert the script value to text.
  373.         OSADisplay( (ComponentInstance) fComponent, fResultValue,
  374.                                     type, kOSAModeNull, newDesc);        
  375.         AEDescToODDesc(newDesc, result);
  376.     CATCH_ALL
  377.         RERAISE;
  378.     ENDTRY
  379. }
  380.  
  381. //------------------------------------------------------------------------------
  382. // Method:        GetResultAsText
  383. //------------------------------------------------------------------------------
  384. ODISOStr CScripter::GetResultAsText( Environment* ev )
  385. {
  386.     ODByteArray     data;
  387.     ODDesc*             rDesc = kODNULL;
  388.     TempODISOStr    result = kODNULL;
  389.     
  390.     ODVolatile(rDesc);
  391.     ODVolatile(result);
  392.     
  393.     TRY
  394.         if ( fResultValue != kOSANullScript )
  395.         {
  396.             rDesc = new ODDesc;
  397.             this->GetResultAsODDesc(typeChar, rDesc );
  398.  
  399.             if ( rDesc->GetDescType(ev) != typeNull )
  400.             {
  401.                 data = rDesc->GetRawData(ev);
  402.                 result = (ODISOStr) ODNewPtr(data._length + 1);
  403.                 ODISOStrNCopy(result, (ODISOStr)data._buffer, data._length);
  404.                 ((char*)result)[data._length] = '\0';
  405.                 
  406.                 ODDeleteObject(rDesc);
  407.             }
  408.         }
  409.     CATCH_ALL
  410.         ODDeleteObject(rDesc);
  411.         RERAISE;
  412.     ENDTRY
  413.  
  414.     return result.DontDelete();
  415. }
  416.  
  417. //------------------------------------------------------------------------------
  418. // Method:        GetError
  419. //------------------------------------------------------------------------------
  420. void CScripter::GetError( ODDescType kind, ODDescType type, ODDesc* result )
  421. {
  422.     TRY
  423.         TempAEDesc newDesc = new AEDesc;
  424.         
  425.         OSAScriptError((ComponentInstance)fComponent, kind, type, newDesc);
  426.         AEDescToODDesc(newDesc, result);
  427.     CATCH_ALL
  428.         RERAISE;
  429.     ENDTRY
  430. }
  431.  
  432. //------------------------------------------------------------------------------
  433. // Method:        GetErrorMessage
  434. //------------------------------------------------------------------------------
  435. ODISOStr CScripter::GetErrorMessage( Environment* ev )
  436. {
  437.     ODByteArray        data;
  438.     ODDesc*             rDesc = kODNULL;
  439.     TempODISOStr    result = kODNULL;
  440.     
  441.     ODVolatile(rDesc);
  442.     ODVolatile(result);
  443.     
  444.     TRY
  445.         rDesc = new ODDesc;
  446.         this->GetError(kOSAErrorMessage, typeChar, rDesc);
  447.         
  448.         data = rDesc->GetRawData(ev);        
  449.         result = (ODISOStr) ODNewPtr(data._length + 1);
  450.         ODISOStrNCopy(result, (ODISOStr)data._buffer, data._length);
  451.         ((char*)result)[data._length] = '\0';
  452.     CATCH_ALL
  453.         if ( result )
  454.         {
  455.             ODDisposePtr(result);
  456.             result = kODNULL;
  457.         }
  458.         RERAISE;
  459.     ENDTRY
  460.     
  461.     ODDeleteObject(rDesc);
  462.  
  463.     return result.DontDelete();
  464. }
  465.  
  466. //------------------------------------------------------------------------------
  467. // Method:        GetErrorAppName
  468. //------------------------------------------------------------------------------
  469. ODISOStr CScripter::GetErrorAppName( Environment* ev )
  470. {
  471.     ODByteArray     data;
  472.     ODDesc*             rDesc = kODNULL;
  473.     TempODISOStr    result = kODNULL;
  474.     
  475.     ODVolatile(rDesc);
  476.     ODVolatile(result);
  477.     
  478.     TRY
  479.         rDesc = new ODDesc;
  480.         this->GetError(kOSAErrorApp, typeChar, rDesc);
  481.         
  482.         data = rDesc->GetRawData(ev);
  483.         result = (ODISOStr) ODNewPtr(data._length + 1);
  484.         ODISOStrNCopy(result, (ODISOStr)data._buffer, data._length);
  485.         ((char*)result)[data._length] = '\0';
  486.     CATCH_ALL
  487.         if ( result )
  488.         {
  489.             ODDisposePtr(result);
  490.             result = kODNULL;
  491.         }
  492.         RERAISE;
  493.     ENDTRY
  494.  
  495.     ODDeleteObject(rDesc);
  496.     
  497.     return result.DontDelete();
  498. }
  499.